package com.dony.shop.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dony.shop.domain.LocalShopItem;
import com.dony.shop.mapper.LocalShopItemMapper;
import com.dony.shop.service.ILocalShopItemService;
import com.dony.shop.util.ShopeeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.*;

/**
 * 订单Service业务层处理
 *
 * @author dony
 * @date 2022-09-07
 */
@Slf4j
@Service("localShopItemServiceImpl")
public class LocalShopItemServiceImpl extends ServiceImpl<LocalShopItemMapper, LocalShopItem> implements ILocalShopItemService {


    @Autowired
    ShopeeUtil shopeeUtil;


    @Override
    public LocalShopItem getItemFromShopee(Long shopId, Long itemId) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("item_id_list", itemId);
        JSONObject response = shopeeUtil.shopGetHandler("/api/v2/product/get_item_base_info", shopId, paramMap);
        if (response == null) {
            log.warn("{}店铺的请求，没有数据返回！", shopId);
            return null;
        }
        List<JSONObject> itemJoList = (List<JSONObject>) response.get("item_list");
        if (itemJoList == null) {
            log.warn("{}的商品查询失败！", itemId);
            return null;
        }
        return null;
    }

    @Override
    public boolean addLocalItemBatch() {
        //获取当指定目录的文件
        List<String> filelist = new LinkedList<>();
        File folder = new File("C:\\Users\\Dominic\\Desktop\\TEMP\\OKG");
        String productName = folder.getName();
        File[] files = folder.listFiles();
        for (File file : files) { // 遍历获取到文件夹中的文件名
            if (file.isFile()) {
                filelist.add(file.getName());
                String imageID = shopeeUtil.uploadFile(file);
                //log.info(uploadFileUrl);
                LocalShopItem item = new LocalShopItem();
                item.setItemName(productName);
                setRandomName(item);
                item.setCategoryId(100574l);
                item.setImage(imageID);
                //"image":{"image_id_list":["33891657a633a87c609afc379e66b19c","2479554ceea3c9ec3e0d218031ea89f3","57b74e018911b4c08740d5e13ae69674"],"image_url_list":["https://cf.shopee.ph/file/33891657a633a87c609afc379e66b19c","https://cf.shopee.ph/file/2479554ceea3c9ec3e0d218031ea89f3","https://cf.shopee.ph/file/57b74e018911b4c08740d5e13ae69674"]}
                item.setOriginalPrice(11000f + RandomUtil.randomInt(500));
                item.setWeight(0.3f);
                item.setShopId(393499517L);
                //调用接口
                if (sendAddMsgToShopee(item)){
                    log.info("添加成功本土商品");
                    file.delete();
                }
            }
        }
        return false;
    }


    /**
     * 随机生成名称,和描述
     * @param item
     * @return
     */
    private void setRandomName(LocalShopItem item){
        String[] modelOptions = {"GLU", "BA", "TYS", "PGD","KS","SED"};
        String model = modelOptions[RandomUtil.randomInt(modelOptions.length)]+RandomUtil.randomInt(10,99)+"-"+RandomUtil.randomInt(100,999);
        String[] nameOptions = {" Colorful", " Multifunctional", " Silicone", " Fashion"," Chronograph"," Calendar"," Sports"," Business"," Couple"," New"," Luminous"," Electronic"," Waterproof"," Outdoor"," Trend"};
        item.setItemName(item.getItemName()+
                nameOptions[RandomUtil.randomInt(nameOptions.length)]+" Watch "+
                model+nameOptions[RandomUtil.randomInt(nameOptions.length)]+" Styles"+
                nameOptions[RandomUtil.randomInt(nameOptions.length)]+
                nameOptions[RandomUtil.randomInt(nameOptions.length)]+" Watch");
        item.setDescription("If you don't find a watch you like in our store, you can send me a photo and I will find you a watch you like！\n\nModel: "+model+"\nSpecial function: luminous\nApplicable people: general\nguarantee: No\nMovement type: Electronic movement\nWatch bottom type: normal\nStrap material: PU\nCase Material : steel\nWatch mirror material: Plastic mirror\nWearing method: wristband");
    }

    /**
     * 请求shopee的接口增加产品
     *
     * @param item
     * @return
     */
    private boolean sendAddMsgToShopee(LocalShopItem item) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("original_price", item.getOriginalPrice());
        paramMap.put("description", item.getDescription());
        paramMap.put("weight", item.getWeight());
        paramMap.put("brand", MapUtil.of("brand_id", 0));

        //设置高宽
        Map<String, Object> dimension = new HashMap<>();
        dimension.put("package_height", 15);
        dimension.put("package_length", 15);
        dimension.put("package_width", 15);
        paramMap.put("dimension", dimension);

        paramMap.put("item_name", item.getItemName());
        paramMap.put("item_sku", "系统500");

        //物流
        Map<String, Object> logisticInfo = new HashMap<>();
        logisticInfo.put("logistic_id", 4000);
        logisticInfo.put("enabled", true);
        Map<String, Object> logisticInfo1 = new HashMap<>();
        logisticInfo1.put("logistic_id", 40021);
        logisticInfo1.put("enabled", true);
        paramMap.put("logistic_info", Arrays.asList(logisticInfo,logisticInfo1));

        //属性
        //Watch Feature
        Map<String, Object> attribute1 = new HashMap<>();
        attribute1.put("attribute_id", 100237);
        List<Object> attributeValueList = new ArrayList();
        attributeValueList.add(MapUtil.of("value_id", 1912));
        attributeValueList.add(MapUtil.of("value_id", 1930));
        attributeValueList.add(MapUtil.of("value_id", 1983));
        attributeValueList.add(MapUtil.of("value_id", 1999));
        attribute1.put("attribute_value_list", attributeValueList);

        //Watch Movement
        Map<String, Object> attribute2 = new HashMap<>();
        attribute2.put("attribute_id", 100244);
        List<Object> attributeValueList2 = new ArrayList();
        attributeValueList2.add(MapUtil.of("value_id", 1920));
        attribute2.put("attribute_value_list", attributeValueList2);

        //Watch Style
        Map<String, Object> attribute3 = new HashMap<>();
        attribute3.put("attribute_id", 100245);
        List<Object> attributeValueList3 = new ArrayList();
        attributeValueList3.add(MapUtil.of("value_id", 1919));
        attributeValueList3.add(MapUtil.of("value_id", 1949));
        attribute3.put("attribute_value_list", attributeValueList3);

        //Strap Material
        Map<String, Object> attribute4 = new HashMap<>();
        attribute4.put("attribute_id", 100230);
        List<Object> attributeValueList4 = new ArrayList();
        attributeValueList4.add(MapUtil.of("value_id", 1937));
        attribute4.put("attribute_value_list", attributeValueList4);


        //Watch Face
        Map<String, Object> attribute5 = new HashMap<>();
        attribute5.put("attribute_id", 100233);
        List<Object> attributeValueList5 = new ArrayList();
        attributeValueList5.add(MapUtil.of("value_id", 1849));
        attribute5.put("attribute_value_list", attributeValueList5);

        List<Object> attributeList = new ArrayList();
        attributeList.add(attribute1);
        attributeList.add(attribute2);
        attributeList.add(attribute3);
        attributeList.add(attribute4);
        attributeList.add(attribute5);

        paramMap.put("attribute_list", attributeList);

        //分类：男表
        paramMap.put("category_id", 100574);

        //图片
        paramMap.put("image", MapUtil.of("image_id_list", Arrays.asList(item.getImage())));

        paramMap.put("seller_stock", Arrays.asList(MapUtil.of("stock", 857)));

        log.info(paramMap.toString());

        JSONObject response = shopeeUtil.shopPostHandler("/api/v2/product/add_item", item.getShopId(), paramMap);
        if (response == null) {
            log.warn("{}店铺的请求失败！", item.getShopId());
            return false;
        }

        item.setItemId(response.getLong("item_id"));
        item.setItemStatus(response.getString("item_status"));
        return this.save(item);
    }


}
